package net.techfuser.storesalereport;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author 左龙龙
 * @Date 21-4-21
 * @Version 1.0
 **/
@Slf4j
public class OrderOplogAnalysis {
    // 有效订单数
    public static final String ORDER_NUM_KEY = "order_num";
    // 优惠金额
    public static final String ORDER_MONEY = "order_money";

    /**
     * 特征检查
     */
    public static void check(JSONObject result, Map<String, Integer> status){
        if(null == result){
            return ;
        }

        if(result.containsKey("confirm_time")){
            status.put(ORDER_NUM_KEY, status.getOrDefault(ORDER_NUM_KEY, 0) + 1);
        }


        if(result.containsKey("cfwc")){
            status.put(ORDER_MONEY, status.getOrDefault(ORDER_MONEY, 0) + 1);
        }
    }


    /**
     * 分析 oplog 记录
     */
    public static Order analysis(List<JSONObject> oplogs){
        Map<String, Integer> status = new HashMap<>();
        JSONObject order = null;
        for(JSONObject row : oplogs){
            String op = row.getString("op");
            if("i".equals(op)){
                order = row.getJSONObject("o");
            }else if("u".equals(op)){
                JSONObject o = row.getJSONObject("o");
                for(String key : o.keySet()){
                    if("$set".equals(key)){
                        order.putAll(o.getJSONObject(key));
                    }else if("$unset".equals(key)){
                        JSONObject unsetObj = o.getJSONObject(key);
                        for(String k : unsetObj.keySet()){
                            order.remove(k);
                        }
                    }else{
                        log.warn("未知的操作: {}", row.toJSONString());
                    }
                }
            }else if("d".equals(op)){
                order = null;
            }else{
                log.warn("未识别的操作: {}", row.toJSONString());
            }

            check(order, status);
        }

        if(order == null){
            log.warn("oplog数据异常", oplogs);
            return null;
        }

        // 未接单,不参与统计
        if(null == order.getDate("confirm_time")){
            return null;
        }

//        // 只测试 jklong 的数据
//        if(1555561823862784900L != order.getLongValue("ent_id")){
//            return null;
//        }


        String order_status = order.getJSONObject("order_status").getString("center");
        int fx = "15".equals(order_status) ? -1 : 1;
        boolean changed = false;
        Order summary = new Order();
        for(String key : status.keySet()){
            Integer count = status.get(key);
            if(count > 1){
                continue;
            }

            switch (key){
                case ORDER_NUM_KEY: {
                    summary.setValid_order_num(1 * fx);
                    break;
                }
                case ORDER_MONEY:{
                    summary.setTotal_original_value(order.getDoubleValue("total_item_yj") * fx);
                    summary.setTotal_sale_value(order.getDoubleValue("total_sale_value") * fx);

                    summary.setTotal_disc_value(order.getDoubleValue("total_disc_value") * fx);
                    summary.setTotal_sj_disc(order.getDoubleValue("total_sj_disc") * fx);
                    summary.setTotal_pt_disc(order.getDoubleValue("total_pt_disc") * fx);

                    summary.setTotal_dp_disc(order.getDoubleValue("total_dp_disc") * fx);
                    summary.setTotal_sj_dp(order.getDoubleValue("total_sj_dp") * fx);
                    summary.setTotal_pt_dp(order.getDoubleValue("total_pt_dp") * fx);

                    summary.setLogistics_value(order.getDoubleValue("logistics_value") * fx);
                    summary.setPackage_fee(order.getDoubleValue("package_fee") * fx);
                    break;
                }
                default: log.warn("数据类型未处理, key: {}", key);
            }

            changed = true;
        }

        if(changed){
            summary.setEnt_id(order.getLongValue("ent_id"));
            summary.setChannel_keyword(order.getString("channel_keyword"));
            summary.setChannel_sheetno(order.getString("channel_sheetno"));
            summary.setRegion_code(order.getString("region_code"));
            summary.setRegion_name(order.getString("region_name"));
            summary.setConfirm_time(order.getDate("confirm_time"));
            summary.setOrder_status(order_status);
        }


        return changed ? summary : null;
    }


}